package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.*;

/**
 * https://leetcode.cn/problems/3sum/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-05-25 16:19
 */
public class Test06 {
    public static void main(String[] args) {
        System.out.println(threeSum(new int[]{-1, 0, 1, 2, -1, -4, 5, -1, 3, 4}));
    }

    public static List<List<Integer>> threeSum(int[] nums) {
        HashSet<List<Integer>> hashSet = new HashSet<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            int target = -nums[i];
            int leftPoint = i + 1;
            int rightPoint = nums.length - 1;
            while (leftPoint < rightPoint) {
                if (leftPoint == i) {
                    leftPoint++;
                    continue;
                }
                if (rightPoint == i) {
                    rightPoint--;
                    continue;
                }
                if (nums[leftPoint] + nums[rightPoint] > target) {
                    rightPoint--;
                } else if (nums[leftPoint] + nums[rightPoint] < target) {
                    leftPoint++;
                } else {
                    List<Integer> temp = new ArrayList<>();
                    temp.add(nums[i]);
                    temp.add(nums[leftPoint]);
                    temp.add(nums[rightPoint]);
                    hashSet.add(temp);
                    leftPoint++;
                    rightPoint = nums.length - 1;
                }


            }
        }

        return new ArrayList<>(hashSet);
    }

}
